/*
 * TestingExcelApp.java
 */

package testingexcel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * The main class of the application.
 */
public class TestingExcelApp extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new TestingExcelView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of TestingExcelApp
     */
    public static TestingExcelApp getApplication() {
        return Application.getInstance(TestingExcelApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(TestingExcelApp.class, args);
    }
    final static int MAX_STUDENTS = 50;
    final static int MIN_STUDENTS = 10;

    public static void getFirstColumn() throws FileNotFoundException,
                                            InvalidFormatException,
                                            IOException
    {
        InputStream inp = new FileInputStream("workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Input Names");
        for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext(); ) {
                Row row = rit.next();
                Cell front = row.getCell(0);
		names.add(front.getStringCellValue());
                names1.add(front.getStringCellValue());
                nameCount++;
	}
        inp.close();

        
    }


    public static void shuffleNames()
    {
        Collections.shuffle(names1);
    }

    public static void printNames()
    {
        
        for (Iterator<String> itr = names.iterator(); itr.hasNext(); )
        {
            String name = itr.next();
            System.out.println(name);
        }
    }

    public static void function1Output() throws FileNotFoundException,
                                            IOException, InvalidFormatException
    {
        InputStream inp = new FileInputStream("workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Randomized Names");
        if (sheet == null)
        {
            sheet = wb.createSheet("Randomized Names");
        }
        for(int i = 0; i < names1.size(); i++)
        {
            Row row = sheet.getRow(i);
            if (row == null)
            {
                row = sheet.createRow(i);
            }
            Cell cell = row.getCell(0);
            if (cell == null)
            {
                cell = row.createCell(0);
            }
            cell.setCellValue(names1.get(i));
        }
        sheet.autoSizeColumn(0);
        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }

    public static ArrayList<SoireeStudent> function3()
    {
        if(nameCount < 13)
        {
            System.out.println("Not enough names to assign 12 unique"
                    + " names to each student.");
             return null;
        }
        ArrayList<SoireeStudent> students =
                new ArrayList<SoireeStudent>(MAX_STUDENTS);
        for(int i = 0; i < names.size(); i++)
        {
            students.add(new SoireeStudent(names.get(i)));
        }
        Random rand = new Random();
        int r;
        for(int i = 0; i < students.size(); i++)
        {
            while (students.get(i).getMet().size() < 12)
            {
                r = rand.nextInt(students.size() - 1);
                if (r >= i)
                {
                    r++;
                }
                if (students.get(r).canBeMet()
                        && !(students.get(i).alreadyMet(students.get(r))))
                {
                    students.get(i).meetSomeone(students.get(r));
                }
            }
        }
        return students;
    }

    public static void function3Output(ArrayList<SoireeStudent> out)
            throws FileNotFoundException, IOException, InvalidFormatException
    {
        InputStream inp = new FileInputStream("workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Soiree Lists");
        if (sheet == null)
        {
            sheet = wb.createSheet("Soiree Lists");
        }        
        for(int i = 0; i < out.size(); i++)
        {
            Row row = sheet.getRow(i);
            if (row == null)
            {
               row = sheet.createRow(i);
            }
            for (int j = 0; j < 13; j++)
            {
                Cell cell = row.getCell(j);
                if (cell == null)
                {
                    cell = row.createCell(j);
                }
                if (j == 0)
                {
                    cell.setCellValue(out.get(i).getName());
                }
                else
                {
                    cell.setCellValue(out.get(i).getMet().get(j - 1));
                }
            }

        }
        for (int i = 0; i < 13; i++)
        {
            sheet.autoSizeColumn(i);
        }

        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }


    public static ArrayList<ProjectGroup>
            function4(ArrayList<String> pairs, int desiredSize)
            {
        int smallerGroups;
        int normalGroups;
        if (nameCount % desiredSize == 0)
        {
            normalGroups = nameCount / desiredSize;
            smallerGroups = 0;
        }
        else
        {
            smallerGroups = desiredSize -  (nameCount % desiredSize);
            normalGroups = nameCount / desiredSize + 1 - smallerGroups;
        }
        ArrayList<String> names4 = new ArrayList<String> (names);
        for (int i = 0; i < pairs.size(); i++)
        {
            names4.remove(pairs.get(i));
        }
        ArrayList<ProjectPair> members = new ArrayList<ProjectPair>(nameCount);
        for (int i = 0; i < pairs.size(); i += 2)
        {
            members.add(new ProjectPair(pairs.get(i), pairs.get(i + 1)));
        }
        for (int i = 0; i < names4.size(); i++)
        {
            members.add(new ProjectPair(names4.get(i)));
        }
        ArrayList<ProjectGroup> groups =
                new ArrayList<ProjectGroup> (smallerGroups + normalGroups);
        for (int i = 0; i < normalGroups; i++)
        {
            groups.add(new ProjectGroup(desiredSize));
        }
        for (int i = 0; i < smallerGroups; i++)
        {
            groups.add(new ProjectGroup(desiredSize - 1));
        }
        Random rand = new Random();
        int r;
        for (int i = 0; i < groups.size(); i++)
        {
            while(!(groups.get(i).isFull()))
            {
                r = rand.nextInt(members.size());
                if (groups.get(i).hasRoom(members.get(r)))
                {
                    groups.get(i).add(members.get(r));
                    members.remove(r);
                }
            }
        }
        return groups;
    }

    public static void function4Output(ArrayList<ProjectGroup> out)
            throws FileNotFoundException, IOException, InvalidFormatException
    {
        for (int i = 0; i < out.size(); i++)
        {
            for (int j = 0; j < out.get(i).getNames().size(); j++)
            {
                System.out.print(out.get(i).getNames().get(j) + " ");
            }
            System.out.println();
        }


        InputStream inp = new FileInputStream("workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Project Groups with Pairs");
        if (sheet == null)
        {
            sheet = wb.createSheet("Project Groups with Pairs");
        }
        for (int i = 0; i < out.size(); i++)
        {
            Row row = sheet.getRow(i);
            if (row == null)
            {
               row = sheet.createRow(i);
            }
            for (int j = 0; j < out.get(i).getNames().size(); j++)
            {
                Cell cell = row.getCell(j);
                if (cell == null)
                {
                    cell = row.createCell(j);
                }
                cell.setCellValue(out.get(i).getNames().get(j));
            }
        }

        for (int i = 0; i < out.get(0).getNames().size(); i++)
        {
            sheet.autoSizeColumn(i);
        }
        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();

    }
    public static void function2Output(ArrayList<ProjectGroup> out)
            throws FileNotFoundException, IOException, InvalidFormatException
    {
        for (int i = 0; i < out.size(); i++)
        {
            for (int j = 0; j < out.get(i).getNames().size(); j++)
            {
                System.out.print(out.get(i).getNames().get(j) + " ");
            }
            System.out.println();
        }


        InputStream inp = new FileInputStream("workbook.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Project Groups");
        if (sheet == null)
        {
            sheet = wb.createSheet("Project Groups");
        }
        for (int i = 0; i < out.size(); i++)
        {
            Row row = sheet.getRow(i);
            if (row == null)
            {
               row = sheet.createRow(i);
            }
            for (int j = 0; j < out.get(i).getNames().size(); j++)
            {
                Cell cell = row.getCell(j);
                if (cell == null)
                {
                    cell = row.createCell(j);
                }
                cell.setCellValue(out.get(i).getNames().get(j));
            }
        }

        for (int i = 0; i < out.get(0).getNames().size(); i++)
        {
            sheet.autoSizeColumn(i);
        }
        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();

    }

    public static ArrayList<String> names = new ArrayList(MAX_STUDENTS);
    public static ArrayList<String> names1 = new ArrayList(MAX_STUDENTS);
    public static int nameCount;

}
